﻿using System;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using SapaFinance.DTO;
using SapaFinance.Services;
using Telerik.Web.Mvc;

namespace SapaFinance.UI.Controllers
{
    public class RequestController : Controller
    {
        private readonly IRequestService _requestService;

        public RequestController(IRequestService requestService)
        {
            _requestService = requestService;
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ru-RU");

        }

        public virtual ActionResult Command(Guid[] checkedRecords, string submitButton)
        {
            switch (submitButton)
            {
                case "CreateOrder":

                    if (checkedRecords != null)
                    {
                        var errors = _requestService.VerifyAggregateToOrder(checkedRecords);
                        if (!errors.Any())
                        {
                            TempData["requests"] = checkedRecords;
                            //чтобы передать массив в CreateOrder, нужно добавить в RouteMap, а пока храним в TempData
                            return this.RedirectToAction<OrderController>(x => x.CreateOrder());
                        }
                        foreach (var error in errors)
                        {
                            ModelState.AddModelError(string.Empty, error);
                        }

                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Ни одна заявка не выбрана");
                    }

                    break;
            }
            return GetRequests();

        }

        public virtual ActionResult Clone(Guid id)
        {
            return View(_requestService.Clone(id));
        }

        public virtual ActionResult InsertClone(RequestDTO dto)
        {
            if (!ModelState.IsValid){
                return View("Clone",dto);
            }
            return Insert(dto);
        }

        public virtual ActionResult GetRequests()
        {
            return View("GetRequests", _requestService.GetListDTO());
        }
        [HttpPost]
        [GridAction]
        public virtual ActionResult Delete(Guid id)
        {
            _requestService.Delete(id);
            return this.RedirectToAction(x => x.GetRequests());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Update(RequestDTO dto)
        {
            dto.CurrentUser = User.Identity.Name;
            if (ModelState.IsValid){
                _requestService.Update(dto);
                return this.RedirectToAction(x => x.GetRequests());
            }
            return GetRequests();
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Insert(RequestDTO dto)
        {
            dto.CurrentUser = User.Identity.Name;
            if(ModelState.IsValid)
            {
                _requestService.Insert(dto);
                return this.RedirectToAction(x => x.GetRequests());
            }
            return GetRequests();
        }

        [HttpPost]
        public ActionResult DisplayCheckedRequests(Guid[] checkedRecords)
        {
            return PartialView("GetCheckedRequests", _requestService.GetListDTO().Where(o => checkedRecords.Contains(o.UID)));
        }
    }

    //public static class ModelStateHelper
    //{
    //    public static IEnumerable Errors(this ModelStateDictionary modelState)
    //    {
    //        if (!modelState.IsValid){
    //            return modelState.ToDictionary(kvp => kvp.Key,
    //                                           kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()).Where(m => m.Value.Count() > 0);
    //        }
    //        return null;
    //    }
    //}
}